{% extends "base.html" %}

{% block title %}任务列表 - 在线跑批系统{% endblock %}

{% block content %}
<div class="d-flex justify-content-between align-items-center mb-4">
    <h1>批处理任务</h1>
    <a href="{% url 'task_add' %}" class="btn btn-primary">
        <i class="fas fa-plus me-1"></i>创建任务
    </a>
</div>

{% if messages %}
{% for message in messages %}
<div class="alert alert-{{ message.tags }} alert-dismissible fade show" role="alert">
    {{ message }}
    <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
{% endfor %}
{% endif %}

<div class="card">
    <div class="card-body">
        <div class="table-responsive">
            <table class="table table-hover table-striped">
                <thead class="table-light">
                    <tr>
                        <th>名称</th>
                        <th>服务器</th>
                        <th>状态</th>
                        <th>最后运行时间</th>
                        <th>是否启用</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    {% for task in tasks %}
                    <tr>
                        <td>{{ task.name }}</td>
                        <td>{{ task.server.name }}</td>
                        <td>
                            {% if task.status == 'running' %}
                            <span class="badge bg-warning">运行中</span>
                            {% elif task.status == 'completed' %}
                            <span class="badge bg-success">已完成</span>
                            {% elif task.status == 'failed' %}
                            <span class="badge bg-danger">已失败</span>
                            {% elif task.status == 'scheduled' %}
                            <span class="badge bg-info">已计划</span>
                            {% else %}
                            <span class="badge bg-secondary">{{ task.get_status_display }}</span>
                            {% endif %}
                        </td>
                        <td>
                            {% if task.last_run_time %}
                            {{ task.last_run_time|date:"Y-m-d H:i" }}
                            {% else %}
                            <span class="text-muted">未运行</span>
                            {% endif %}
                        </td>
                        <td>
                            {% if task.is_enabled %}
                            <span class="badge bg-success">启用</span>
                            {% else %}
                            <span class="badge bg-secondary">禁用</span>
                            {% endif %}
                        </td>
                        <td>
                            <div class="btn-group btn-group-sm">
                                <!-- 关键：传递 task.id（整数主键，匹配 URL 的 <int:pk>） -->
                                {% if task.status != 'running' and task.id %} <!-- 额外判断 task.id 存在 -->
                                <a href="{% url 'run_task' task.id %}" class="btn btn-outline-success run-task-btn">
                                    <i class="fas fa-play"></i> 运行
                                </a>
                                {% endif %}
                                <!-- 正确代码（替换后） -->
                                {% if task.latest_log %} <!-- 直接判断是否有最新日志（更安全） -->
                                <a href="{% url 'task_log' task.latest_log.id %}" class="btn btn-outline-info">
                                    <i class="fas fa-history"></i> 日志
                                </a>
                                {% endif %}
                                {% if task.status != 'running' and task.id %}
                                <a href="{% url 'task_edit' task.id %}" class="btn btn-outline-primary">
                                    <i class="fas fa-edit"></i> 编辑
                                </a>
                                {% endif %}
                                {% if task.status != 'running' and task.id %}
                                <a href="{% url 'task_delete' task.id %}" class="btn btn-outline-danger delete-btn"
                                    data-item-name="任务 {{ task.name }}">
                                    <i class="fas fa-trash"></i> 删除
                                </a>
                                {% endif %}
                            </div>
                        </td>
                    </tr>
                    {% empty %}
                    <tr>
                        <td colspan="6" class="text-center text-muted py-3">
                            <p class="mb-0">暂无任务数据</p>
                            <a href="{% url 'task_add' %}" class="btn btn-sm btn-primary mt-2">创建第一个任务</a>
                        </td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
    </div>
</div>
{% endblock %}